<template>
  <div class="hello">
    <img src="../assets/logo.png">
    <div>
      <h1>启迪面试系统</h1>
      <div id="signup">

    <el-card class="box-card">
    <div slot="header" class="clearfix">
    <h2>用户注册 <router-link to="/login" type="primary" class="a_login">我要登录</router-link></h2>
    </div>
    <div class="text item">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"> 
          <el-form-item label="登录账号" prop="username">
            <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户昵称" prop="usernick">
            <el-input type="text" v-model="ruleForm.usernick" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="登录密码" prop="userpass">
            <el-input type="password" v-model="ruleForm.userpass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkpass">
            <el-input type="password" v-model="ruleForm.checkpass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">我要注册</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
    </div>
    </el-card>

        
      </div>
    </div>
  </div>
</template>

<script>
export default {
    data() {
      var validateName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
          callback();
        }
      };
      var validateNick = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入昵称'));
        } else {
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkpass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.userpass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          username: '',
          usernick: '',
          userpass: '',
          checkpass: ''
        },
        rules: {
          username: [
            {validator: validateName, trigger: 'blur' }
          ],
          usernick: [
            {validator: validateNick, trigger: 'blur' }
          ],
          userpass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkpass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}

#signup {
  width: 400px;
  margin: 0 auto;
}

.a_login {
  font-size: 12px;
}
</style>
